package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.order.OrderUtils;
import com.ruoyi.statistic.domain.TMonthProfitBillInfo;
import com.ruoyi.statistic.service.ITMonthProfitBillInfoService;
import com.ruoyi.statistic.service.ITMonthProfitWaybillRelService;
import com.ruoyi.waybill.domain.TWayBillInfo;
import com.ruoyi.waybill.service.ITWayBillInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务，利润账单，任务
 * 
 * @author ruoyi
 */
@Component("pbTask")
public class PbTask
{
    @Autowired
    private ITWayBillInfoService tWayBillInfoService;

    @Autowired
    private ITMonthProfitBillInfoService tMonthProfitBillInfoService;

    @Autowired
    private ITMonthProfitWaybillRelService tMonthProfitWaybillRelService;

    private static Logger logger = LoggerFactory.getLogger(PbTask.class);

    /**
     * 统计月利润单，无参
     */

    public void statisticMonthProfitBill(String strProfitBillDate)
    {
        logger.debug("统计，月利润单，月账单，按结算客户，开始 ...");
        /**
         * 计算当前日期，前一月日期
         */
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.now();
        if(StringUtils.isNotEmpty(strProfitBillDate)) {
            currentDate = LocalDate.parse(strProfitBillDate, formatter);
        }
        logger.debug("当前日期: " + currentDate);

        LocalDate lastMonthDate = currentDate.minusMonths(1);
        logger.debug("上月日期: " + lastMonthDate);
        LocalDate lastMonthFirstDay = lastMonthDate.with(TemporalAdjusters.firstDayOfMonth());
        logger.debug("上个月第一天：" + lastMonthFirstDay);
        LocalDate lastMonthLastDay = lastMonthDate.with(TemporalAdjusters.lastDayOfMonth());
        logger.debug("上个月最后一天：" + lastMonthLastDay);
        Date beginDate = DateUtils.toDate(lastMonthFirstDay);
        Date endDate = DateUtils.toDate(lastMonthLastDay);

        Integer monthTripNumber = 0;
        Double monthFreightWeight = 0.0;
        Double monthSettlementAmount = 0.0; //主货运费(元)
        //Double monthPremium = 0.0; //运费(元)
        Double monthMinorSettlementAmount = 0.0; //配货运费(元)
        Double monthSettlementTotalAmount = 0.0;

        Double monthCashRefuel = 0.0;
        Double monthDesignateRefuel = 0.0;
        Double monthTollFeeCash = 0.0;
        Double monthTollFeeEtc = 0.0;
        Double monthForfeit = 0.0;
        Double monthInformationFee = 0.0;
        Double monthRepairTruckFee = 0.0;
        Double monthServiceStationRepair = 0.0;
        Double monthTireFee = 0.0;
        Double monthOtherFee = 0.0;
        Double monthInsurancePremium = 0.0;
        Double monthVehicleVesselTax = 0.0;
        Double monthGreatDipperFee = 0.0;
        Double monthGasCylinderFee = 0.0;
        Double monthExpenseTotal = 0.0;
        Double monthGrossProfit = 0.0;
        Double monthFreightAmount = 0.0; //主货工资
        //Double monthPrimage = 0.0;
        Double monthMinorFreightAmount = 0.0; //配货工资
        Double monthFreightTotalAmount = 0.0;
        Double monthProfit = 0.0;

        TMonthProfitBillInfo tMonthProfitBillInfo = new TMonthProfitBillInfo();
        String monthProfitBillNumber;
        Long monthProfitBillId = 0L;
        List<Long> wayBillIds = new ArrayList<>();

        /**
         * 按录单日期，筛选车牌号码
         */
        List<String> vehicleLicenseNumbers = tWayBillInfoService.selectProfitStatisticFieldByRecordDate("自有车辆", beginDate, endDate);
        logger.debug("车牌号码列表：vehicleLicenseNumbers = " + vehicleLicenseNumbers);

        /**
         * 遍历车牌号码，运单
         */
        for(String vehicleLicenseNumber : vehicleLicenseNumbers)
        {
            /**
             * 按车牌号。录单日期，查询运单
             */
            //tWayBillExt.setFreightSettlementCustomer(vehicleLicenseNumber);
            logger.debug("车牌号码：vehicleLicenseNumber = " + vehicleLicenseNumber);
            List<TWayBillInfo> wayBillInfos = tWayBillInfoService.selectTWayBillInfoUsedByProfitStatistic(vehicleLicenseNumber, beginDate, endDate);
            //logger.debug("运单列表：wayBillInfos = " + wayBillInfos);
            /**
             * 合计利润，记录运单ID
             */
            for(TWayBillInfo wayBillInfo : wayBillInfos) {
                monthTripNumber = monthTripNumber + 1;
                monthFreightWeight = monthFreightWeight + wayBillInfo.getFreightWeight();
                monthSettlementAmount = monthSettlementAmount + wayBillInfo.getSettlementAmount(); //运费(元)
                //monthPremium = monthPremium + wayBillInfo.getPremium();
                monthMinorSettlementAmount = monthMinorSettlementAmount + wayBillInfo.getMinorSettlementAmount();
                monthSettlementTotalAmount = monthSettlementTotalAmount + wayBillInfo.getSettlementTotalAmount();

                monthCashRefuel = monthCashRefuel + wayBillInfo.getCashRefuel();
                monthDesignateRefuel = monthDesignateRefuel + wayBillInfo.getDesignateRefuel();
                monthTollFeeCash = monthTollFeeCash + wayBillInfo.getTollFeeCash();
                monthTollFeeEtc = monthTollFeeEtc + wayBillInfo.getTollFeeEtc();
                monthForfeit = monthForfeit + wayBillInfo.getForfeit();
                monthInformationFee = monthInformationFee + wayBillInfo.getInformationFee() + wayBillInfo.getMinorInformationFee();
                monthRepairTruckFee = monthRepairTruckFee + wayBillInfo.getRepairTruckFee();
                monthServiceStationRepair = monthServiceStationRepair + wayBillInfo.getServiceStationRepair();
                monthTireFee = monthTireFee + wayBillInfo.getTireFee();
                monthOtherFee = monthOtherFee + wayBillInfo.getOtherFee();
                monthInsurancePremium = monthInsurancePremium + wayBillInfo.getInsurancePremium();
                monthVehicleVesselTax = monthVehicleVesselTax + wayBillInfo.getVehicleVesselTax();
                monthGreatDipperFee = monthGreatDipperFee + wayBillInfo.getGreatDipperFee();
                monthGasCylinderFee = monthGasCylinderFee + wayBillInfo.getGasCylinderFee();
                monthExpenseTotal = monthExpenseTotal + wayBillInfo.getExpenseTotal();
                // monthGrossProfit，不用累计
                monthFreightAmount = monthFreightAmount + wayBillInfo.getFreightAmount(); //主货工资
                //monthPrimage = monthPrimage + wayBillInfo.getPrimage(); //司机附加运费，小货分润
                monthMinorFreightAmount = monthMinorFreightAmount + wayBillInfo.getMinorFreightAmount(); //配货工资
                monthFreightTotalAmount = monthFreightTotalAmount + wayBillInfo.getFreightTotalAmount();
                //monthProfit，不用累计
                wayBillIds.add(wayBillInfo.getWayBillId());
            }
            //毛利计算
            monthGrossProfit = monthSettlementTotalAmount - monthCashRefuel - monthDesignateRefuel - monthTollFeeCash
                - monthTollFeeEtc - monthForfeit - monthInformationFee - monthRepairTruckFee - monthServiceStationRepair
                - monthTireFee - monthOtherFee - monthInsurancePremium - monthVehicleVesselTax - monthGreatDipperFee
                - monthGasCylinderFee;
            //利润计算
            monthProfit = monthGrossProfit - monthFreightTotalAmount;

            /**
             * 月利润单编号
             */
            monthProfitBillNumber = OrderUtils.getOrderNumber("MP", "-");

            /**
             * 利润总额，四舍五入，保留2位小数
             */
            monthFreightWeight = Math.round(monthFreightWeight * 1000.0) / 1000.0;
            monthSettlementAmount = Math.round(monthSettlementAmount * 100.0) / 100.0; //运费(元)
            //monthPremium = Math.round(monthPremium * 100.0) / 100.0;
            monthMinorSettlementAmount = Math.round(monthMinorSettlementAmount * 100.0) / 100.0;
            monthSettlementTotalAmount = Math.round(monthSettlementTotalAmount * 100.0) / 100.0;

            monthCashRefuel = Math.round(monthCashRefuel * 100.0) / 100.0;
            monthDesignateRefuel = Math.round(monthDesignateRefuel * 100.0) / 100.0;
            monthTollFeeCash = Math.round(monthTollFeeCash * 100.0) / 100.0;
            monthTollFeeEtc = Math.round(monthTollFeeEtc * 100.0) / 100.0;
            monthForfeit = Math.round(monthForfeit * 100.0) / 100.0;
            monthInformationFee = Math.round(monthInformationFee * 100.0) / 100.0;
            monthRepairTruckFee = Math.round(monthRepairTruckFee * 100.0) / 100.0;
            monthServiceStationRepair = Math.round(monthServiceStationRepair * 100.0) / 100.0;
            monthTireFee = Math.round(monthTireFee * 100.0) / 100.0;
            monthOtherFee = Math.round(monthOtherFee * 100.0) / 100.0;
            monthInsurancePremium = Math.round(monthInsurancePremium * 100.0) / 100.0;
            monthVehicleVesselTax = Math.round(monthVehicleVesselTax * 100.0) / 100.0;
            monthGreatDipperFee = Math.round(monthGreatDipperFee * 100.0) / 100.0;
            monthGasCylinderFee = Math.round(monthGasCylinderFee * 100.0) / 100.0;
            monthExpenseTotal = Math.round(monthExpenseTotal * 100.0) / 100.0;
            monthGrossProfit = Math.round(monthGrossProfit * 100.0) / 100.0;
            monthFreightAmount = Math.round(monthFreightAmount * 100.0) / 100.0; //主货工资
            //monthPrimage = Math.round(monthPrimage * 100.0) / 100.0; //司机附加运费，小货分润
            monthMinorFreightAmount = Math.round(monthMinorFreightAmount * 100.0) / 100.0; //配货工资
            monthFreightTotalAmount = Math.round(monthFreightTotalAmount * 100.0) / 100.0;
            monthProfit = Math.round(monthProfit * 100.0) / 100.0;

            /**
             * 月利润单，数据准备
             */
            tMonthProfitBillInfo.setProfitBillNumber(monthProfitBillNumber);
            tMonthProfitBillInfo.setProfitBillMonth(DateUtils.toDate(lastMonthDate));
            tMonthProfitBillInfo.setVehicleLicenseNumber(vehicleLicenseNumber);
            tMonthProfitBillInfo.setVehicleMark("自有车辆");
            // 按趟计算，除以2
            monthTripNumber = monthTripNumber/2;
            tMonthProfitBillInfo.setTripNumber(monthTripNumber);
            tMonthProfitBillInfo.setFreightWeight(monthFreightWeight);
            tMonthProfitBillInfo.setSettlementAmount(monthSettlementAmount); //运费(元)
            //tMonthProfitBillInfo.setPremium(monthPremium);
            tMonthProfitBillInfo.setPremium(monthMinorSettlementAmount);
            tMonthProfitBillInfo.setSettlementTotalAmount(monthSettlementTotalAmount);
            tMonthProfitBillInfo.setCashRefuel(monthCashRefuel);
            tMonthProfitBillInfo.setDesignateRefuel(monthDesignateRefuel);
            tMonthProfitBillInfo.setTollFeeCash(monthTollFeeCash);
            tMonthProfitBillInfo.setTollFeeEtc(monthTollFeeEtc);
            tMonthProfitBillInfo.setForfeit(monthForfeit);
            tMonthProfitBillInfo.setInformationFee(monthInformationFee);
            tMonthProfitBillInfo.setRepairTruckFee(monthRepairTruckFee);
            tMonthProfitBillInfo.setServiceStationRepair(monthServiceStationRepair);
            tMonthProfitBillInfo.setTireFee(monthTireFee);
            tMonthProfitBillInfo.setOtherFee(monthOtherFee);
            tMonthProfitBillInfo.setInsurancePremium(monthInsurancePremium);
            tMonthProfitBillInfo.setVehicleVesselTax(monthVehicleVesselTax);
            tMonthProfitBillInfo.setGreatDipperFee(monthGreatDipperFee);
            tMonthProfitBillInfo.setGasCylinderFee(monthGasCylinderFee);
            tMonthProfitBillInfo.setExpenseTotal(monthExpenseTotal);
            tMonthProfitBillInfo.setGrossProfit(monthGrossProfit);
            tMonthProfitBillInfo.setFreightAmount(monthFreightAmount); //主货工资
            //tMonthProfitBillInfo.setPrimage(monthPrimage); //司机附加运费，小货分润
            tMonthProfitBillInfo.setPrimage(monthMinorFreightAmount); //配货工资
            tMonthProfitBillInfo.setFreightTotalAmount(monthFreightTotalAmount);
            tMonthProfitBillInfo.setProfit(monthProfit);

            tMonthProfitBillInfo.setCreateBy("PbTask");
            tMonthProfitBillInfo.setCreateTime(DateUtils.getNowDate());

            /**
             * 月利润单，插入数据
             */
            tMonthProfitBillInfoService.insertTMonthProfitBillInfo(tMonthProfitBillInfo);

            /**
             * 获取插入记录，ID
             */
            monthProfitBillId = tMonthProfitBillInfo.getProfitBillId();
            logger.debug("OOO " + "monthProfitBillId = " + monthProfitBillId);

            /**
             * monthFreightAmount清零
             */
            monthTripNumber = 0;
            monthFreightWeight = 0.0;
            monthSettlementAmount = 0.0; //运费(元)
            monthMinorSettlementAmount = 0.0; //运费(元)
            monthSettlementTotalAmount = 0.0;

            monthCashRefuel = 0.0;
            monthDesignateRefuel = 0.0;
            monthTollFeeCash = 0.0;
            monthTollFeeEtc = 0.0;
            monthForfeit = 0.0;
            monthInformationFee = 0.0;
            monthRepairTruckFee = 0.0;
            monthServiceStationRepair = 0.0;
            monthTireFee = 0.0;
            monthOtherFee = 0.0;
            monthInsurancePremium = 0.0;
            monthVehicleVesselTax = 0.0;
            monthGreatDipperFee = 0.0;
            monthGasCylinderFee = 0.0;
            monthExpenseTotal = 0.0;
            monthGrossProfit = 0.0;
            monthFreightAmount = 0.0; //主货工资
            //monthPrimage = 0.0;
            monthMinorFreightAmount = 0.0; //配货工资
            monthFreightTotalAmount = 0.0;
            monthProfit = 0.0;

            /**
             * 利润运单关联表，数据插入
             */
            logger.debug("OOO " + "wayBillIds = " + wayBillIds);
            tMonthProfitWaybillRelService.insertTMonthProfitWayBillRelExt(monthProfitBillId, wayBillIds);
            wayBillIds.clear();
        }
        logger.debug("统计，月利润单，月账单，按结算客户，结束 ...");
    }

}
